package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import app.Disthanitian.thaistyle.EmerMez.RiskZoneAll.WebPageTask;

public class SearchRiskZone {

	
	ArrayList<RiskZone> riskFromDB;
	RiskZone r;
	InputStream is = null;
	String js_result = "";
	HttpPost httppost = null;
	MemberUser user = new MemberUser();
	static UrlEmerMez URLlink = new UrlEmerMez();
	public static final String urlSearchRiskZoneByUserIdAndRiskId= URLlink.getUrlsearchriskzonebyuserid();
	public static String result = "";	 
	String userId;
	String riskId;
	
	
	public void callRun() {
		SearchRiskZoneByRiskIdAndUserId task = new SearchRiskZoneByRiskIdAndUserId();
		task.execute();		
	}
	
	public void setVariable(String userId){
		this.userId= userId;
	
	}

	public ArrayList<RiskZone> getRiskFromDB() {
		return riskFromDB;
	}
	
	public ArrayList<RiskZone> getAllRiskZone() {
		ArrayList<RiskZone> values = new ArrayList<RiskZone>();		
		Log.d("result",""+result.length());
		try {

			JSONArray jArray = new JSONArray(result);
			Log.d("ValueArray1",""+result.length());
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject jo = jArray.getJSONObject(i);

				String risk_id = jo.getString("risk_id");
				String user_id = jo.getString("user_id");
				String nickName = jo.getString("risk_nickname");
				String enable = jo.getString("user_id");
				
				
				r.setId(risk_id);
				r.setUserId(user_id);
				r.setNickName(nickName);
				r.setEnable(enable);
				values.add(r);
				
				Log.d("ValueArray",""+values.size());
				
			}
			Log.d("ValueArray",""+values.size());
		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
			Log.d("LoveLog","Error parsing data " + e.toString());

		}
		
		return values;
		
		
	}
	public String ShowRiskZone() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("userId", userId ));
		nameValuePairs.add(new BasicNameValuePair("riskId", riskId ));
		Log.d("Hello",""+userId);
		return postData(urlSearchRiskZoneByUserIdAndRiskId, nameValuePairs);
	}

	public String postData(String urlShow,List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(urlShow);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");

		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			String line;
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			js_result = sb.toString();
			return js_result;
		} catch (ClientProtocolException e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		} catch (IOException e) {
		}
		return null;
	}

	/**
	 * @author Living_Room
	 *
	 */
	protected class SearchRiskZoneByRiskIdAndUserId extends AsyncTask<Void, Void, String> {
		
		@Override
		protected String doInBackground(Void... params) {
			result = ShowRiskZone();
			RiskZoneAll risk = new RiskZoneAll();			
			riskFromDB = risk.getAllRiskZone();
			Log.d("Riskss",""+risk.getAllRiskZone().size());
			return result;
		}

		protected void onPostExecute(String result) {
			

		}
		

	}
}
	
